'use strict';

const Controller = require('egg').Controller;

class contrl extends Controller {
    async add(){
        const {ctx,app,service} = this;
        var name = ctx.request.body.name || "";
        if(!name){
            ctx.error(app.resCode.params);
        }else{
            try{
                let param = ctx.request.body;
                param.uid = ctx.session.userinfo._id;
                let res = await service.tag.add(param);
                ctx.success(res);
            }catch(e){
                ctx.error(app.resCode.params,e.message || e);
            }
        }
    }
    async remove(){
        const {ctx,app,service} = this;
        let id = ctx.request.body.id || "";
        if(!id){
         ctx.error(app.resCode.params);
        }else{
             try{
                 let res = await service.tag.remove(id);
                 ctx.success(res);
             }catch(e){
                 ctx.error(app.resCode.params,e.message || e);
             }
        }
    }
    async update(){
        const {ctx,app,service} = this;
        let param = ctx.request.body;
        let id = param.id;
        let name = param.name || "";
        let uid = ctx.session.userinfo._id;
        if(!id || !name){
            ctx.error(app.resCode.params);
        }else{
            try{
                let res = await service.tag.update({id,name,uid});
                ctx.success(res);
            }catch(e){
                ctx.error(app.resCode.params,e.message || e);
            }
        }
   }
   async list(){
    const {ctx,app,service} = this;
    try{
        let res = await service.tag.list(ctx.session.userinfo._id);
        ctx.success(res);
    }catch(e){
        ctx.error(app.resCode.params,e.message || e);
    }
   }
}

module.exports = contrl;